// pages/manager/all/all.js
// var list = require("../../../testdata.js");
var gd = getApp().globalData;
const net = require("../../../utils/net.js");
const util = require("../../../utils/util.js");
var posting = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticData:{
      month_sales_volume: 0,
      today_sales_volume: 0,
      recent_expire: 0,
      count_expire: 0,
      money_expire: 0,
      no_arrival_long_time: 0,
      no_followup_long_time:0
    },
    list:[],
    page:1,
    enableshow:false,
    enable:0,
    source:[
      "所有", "有效", "未购卡"
    ], scrollTop: 0,
    scrolltmp: 0,
    nomore:false,
    isfirstpost:false
  }, 
  pickerChange(e){
    console.log(e);
    if (this.data.enable != e.detail.value){
      this.setData({
        list:[],
        page:1,
        enable: e.detail.value
      })
      this.getList();
    }else{
      this.setData({
        enable: e.detail.value
      })
    }

  },
  navgo(e){
    setTimeout(function(){
      util.Forward(e.currentTarget.dataset.route);
    },100);
  },
  firstData(){
    var that = this;
    net.request({
      url: '/StatisticData',
      data:{
        id: gd.id,
        shop_id: gd.shop_id
      },success(res){
        if(res.data.flag){
          that.setData({
            staticData:res.data.data
          })
        }else{
          // if (dt.msg.indexOf("没有更多") >= 0) {
          //   that.setData({
          //     nomore: true,
          //     list: list
          //   })
          //   this.dialog.showDialog({
          //     msg: "没有更多"
          //   })
          // }
        }
      },complete(){
      }
    })
    that.getList();
  },
  getList(){
    var that = this;
    // if (that.data.nomore) {
    //   this.dialog.showDialog({
    //     msg: "没有更多"
    //   })
    //   return;
    // }
    if (posting) return;
    posting = true;
    net.request({
      url: '/MemberList',
      data: {
        id: gd.id,
        shop_id: gd.shop_id,
        search_type: 0,
        page: that.data.page,
        status: that.data.enable*1
      }, success(res) {
        var page = that.data.page;
        var list = that.data.list;
        if (res.data.flag){
          if(res.data.data.length>0){
            page+=1;
            for(var i=0;i<res.data.data.length;++i){
              list.push(res.data.data[i]);
            }
          }else{
          }
          that.setData({
            list: list,
            page: page
          })
        }else{
          wx.hideLoading();

          that.dialog.showDialog({
            msg: res.data.msg
          })
        }
      }, complete() {
        wx.stopPullDownRefresh();
        wx.hideLoading();
        posting = false;
        that.scrollloading(false);
      }
    })
    wx.showLoading({})
  },
  toscroll(e) {
    this.setData({
      scrolltmp: e.detail.scrollTop
    })
  }, tolower(){
    this.scrollloading(true);
    this.getList();
  },
  scrollloading(isshow) {
    var that = this;
    let top = 0;
    let delay = 0;
    if (isshow){
      top = that.data.list.length*50;
    }else{
      delay = 100;
      top = that.data.scrolltmp - 50;
    }
    setTimeout(function () {
      that.setData({
        scrollTop: top
      })
    }, delay);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.firstData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.dialog = this.selectComponent("#dialog");
    // this.dialog.showDialog({
    //   msg: "没有更多"
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    if(wx.getStorageSync("managerdatachange")){
      this.setData({
        list:[],
        page:1
      })
      this.firstData();
      wx.setStorageSync("managerdatachange",false);
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.firstData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})